package com.example.userpage.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserVotesRepository {
    @Autowired
    private JdbcTemplate jdbcLogs;

    public Map<String, Object> findByUsername(String username) {
        String sql = "SELECT vote_time, vote_num FROM user_votes WHERE username = ?";
        List<Map<String, Object>> results = jdbcLogs.queryForList(sql, username);
        if (results.isEmpty()) {
            // 没有投票记录时返回默认值
            Map<String, Object> defaultMap = new HashMap<>();
            defaultMap.put("vote_time", "");
            defaultMap.put("vote_num", 0);
            return defaultMap;
        }
        return results.get(0);
    }
}
